@@ -2,28 +2,51 @@ module Agents |
||
2 | 2 |
class WebhookAgent < Agent |
3 | 3 |
cannot_be_scheduled! |
4 | 4 |
|
5 |
- description <<-MD |
|
6 |
- Use this Agent to create events by receiving webhooks from any source. |
|
5 |
+ description do |
|
6 |
+ <<-MD |
|
7 |
+ Use this Agent to create events by receiving webhooks from any source. |
|
7 | 8 |
|
8 |
- Options: |
|
9 |
+ In order to create events with this agent, make a POST request to: |
|
10 |
+ ``` |
|
11 |
+ https://#{ENV['DOMAIN']}/users/#{user.id}/webhooks/#{id || '<id>'}/:secret |
|
12 |
+ ``` where `:secret` is specified in your options. |
|
9 | 13 |
|
10 |
- * `secret` - A token that the host will provide for authentication. |
|
11 |
- MD |
|
14 |
+ The |
|
15 |
+ |
|
16 |
+ Options: |
|
17 |
+ |
|
18 |
+ * `secret` - A token that the host will provide for authentication. |
|
19 |
+ * `expected_receive_period_in_days` - How often you expect to receive |
|
20 |
+ events this way. Used to determine if the agent is working. |
|
21 |
+ * `payload_path` - JSONPath of the attribute of the POST body to be |
|
22 |
+ used as the Event payload. |
|
23 |
+ MD |
|
24 |
+ end |
|
25 |
+ |
|
26 |
+ event_description do |
|
27 |
+ <<-MD |
|
28 |
+ The event payload is base on the value of the `payload_path` option, |
|
29 |
+ which is set to `#{options[:payload_path]}`. |
|
30 |
+ MD |
|
31 |
+ end |
|
12 | 32 |
|
13 | 33 |
def default_options |
14 |
- { "secret" => "supersecretstring", } |
|
34 |
+ { "secret" => "supersecretstring", |
|
35 |
+ "expected_receive_period_in_days" => 1, |
|
36 |
+ "payload_path" => "payload"} |
|
15 | 37 |
end |
16 | 38 |
|
17 | 39 |
def receive_webhook(params) |
18 |
- return ["Not Authorized", 401] unless params[:secret] == options[:secret] |
|
40 |
+ secret = params.delete(:secret) |
|
41 |
+ return ["Not Authorized", 401] unless secret == options[:secret] |
|
19 | 42 |
|
20 |
- create_event(:payload => params[:payload]) |
|
43 |
+ create_event(:payload => payload_for(params)) |
|
21 | 44 |
|
22 | 45 |
['Event Created', 201] |
23 | 46 |
end |
24 | 47 |
|
25 | 48 |
def working? |
26 |
- true |
|
49 |
+ event_created_within(options[:expected_receive_period_in_days]) && !recent_error_logs? |
|
27 | 50 |
end |
28 | 51 |
|
29 | 52 |
def validate_options |
@@ -31,5 +54,9 @@ module Agents |
||
31 | 54 |
errors.add(:base, "Must specify a :secret for 'Authenticating' requests") |
32 | 55 |
end |
33 | 56 |
end |
57 |
+ |
|
58 |
+ def payload_for(params) |
|
59 |
+ Utils.values_at(params, options[:payload_path]) || {} |
|
60 |
+ end |
|
34 | 61 |
end |
35 | 62 |
end |
@@ -3,11 +3,12 @@ require 'spec_helper' |
||
3 | 3 |
describe Agents::WebhookAgent do |
4 | 4 |
let(:agent) do |
5 | 5 |
_agent = Agents::WebhookAgent.new(:name => 'webhook', |
6 |
- :options => {:secret => :foobar}) |
|
6 |
+ :options => {:secret => :foobar, :payload_path => '$'}) |
|
7 | 7 |
_agent.user = users(:bob) |
8 | 8 |
_agent.save! |
9 | 9 |
_agent |
10 | 10 |
end |
11 |
+ let(:payload) { {:some => :info} } |
|
11 | 12 |
|
12 | 13 |
after { agent.destroy } |
13 | 14 |
|
@@ -15,17 +16,19 @@ describe Agents::WebhookAgent do |
||
15 | 16 |
it 'should create event if secret matches' do |
16 | 17 |
out = nil |
17 | 18 |
lambda { |
18 |
- out = agent.receive_webhook({:secret => :foobar, :payload => {:some => :info}}) |
|
19 |
+ out = agent.receive_webhook({:secret => :foobar, :payload => payload}) |
|
19 | 20 |
}.should change { Event.count }.by(1) |
20 | 21 |
out.should eq(['Event Created', 201]) |
22 |
+ Event.last.last.payload.should eq([{'payload' => payload}]) |
|
21 | 23 |
end |
22 | 24 |
|
23 | 25 |
it 'should not create event if secrets dont match' do |
24 | 26 |
out = nil |
25 | 27 |
lambda { |
26 |
- out = agent.receive_webhook({:secret => :bazbat, :payload => {:some => :info}}) |
|
28 |
+ out = agent.receive_webhook({:secret => :bazbat, :payload => payload}) |
|
27 | 29 |
}.should change { Event.count }.by(0) |
28 | 30 |
out.should eq(['Not Authorized', 401]) |
31 |
+ Event.last.last.payload.should eq([{'payload' => payload}]) |
|
29 | 32 |
end |
30 | 33 |
end |
31 | 34 |
end |